package com.classic.download;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.classic.base.BaseActivity;
import com.classic.duanzhi.R;

import java.util.ArrayList;
import java.util.List;

public class ShowAppListActivity extends BaseActivity {

    @Override
    protected int getLayoutId() {
        return R.layout.activity_show_app_list;
    }

    RecyclerView list;

    @Override
    protected void initView() {
        list = findViewById(R.id.rv_list);
        list.setLayoutManager(new LinearLayoutManager(this));
        Adapter adapter = new Adapter();
        list.setAdapter(adapter);
        adapter.setData(AppUtils.getAppList());

    }

    class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {

        @NonNull
        @Override
        public Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(getLayoutInflater().inflate(R.layout.u_item_app_layout, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {
            holder.initView(position);
        }

        @Override
        public int getItemCount() {
            return apps.size();
        }

        List<APP> apps = new ArrayList<>();

        public void setData(List<APP> apps) {
            this.apps = apps;
            notifyDataSetChanged();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            ImageView ivLogo;
            TextView appname, banben;
            LinearLayout itemLayout;

            public ViewHolder(View itemView) {
                super(itemView);
                ivLogo = itemView.findViewById(R.id.iv_logo);
                appname = itemView.findViewById(R.id.tv_appname);
                banben = itemView.findViewById(R.id.tv_banben);
                itemLayout = itemView.findViewById(R.id.item_layout);
            }

            public void initView(int position) {
                final APP app = apps.get(position);
                Glide.with(ShowAppListActivity.this).load(app.getLogo()).into(ivLogo);
                appname.setText(app.getName());
                banben.setText(app.getVersions());
                itemLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        AppDetailsActivity.starThis(ShowAppListActivity.this,app.getName());
                    }
                });
            }
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public String getActivityTitle() {
        return "APP分享";
    }

}
